﻿using System;
using System.Linq;

namespace Microsoft.Extensions.DependencyInjection
{
    public sealed class LocalFileStorageOptions
    {
        /// <summary>
        /// 文件存储根目录
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// 允许存储单个文件的大小上限，如：1K、2M、3G
        /// </summary>
        public string MaxLength { get; set; } = "10M";

        public long GetLength()
        {
            var unitChar = MaxLength[^1].ToString().ToLower();
            long unit;
            switch (unitChar)
            {
                case "k":
                    unit = 1;
                    break;
                case "m":
                    unit = 2;
                    break;

                case "g":
                    unit = 3;
                    break;
                default: throw new ArgumentOutOfRangeException();
            }

            if (long.TryParse(MaxLength[..^1], out var value))
            {
                return (long)(value * Math.Pow(1024L, unit));
            }
            throw new ArgumentException($"MaxLength的值{MaxLength}无效");
        }
    }
}